<?php
class CommonAction extends Action {

    public function _initialize() {
        $this->_showNav();
        $this->_showFlink();
        $this->_showSetting();
        $this->_showNow();
    }

    protected function showAbout() {
        //显示关于信息
        $about = M('About');
        $gonggao = $about->field(array('id', 'title'))
                         ->where('type=1')
                         ->limit(5)
                         ->select();

        $help = $about->field(array('id', 'title'))
                      ->where('type=2')
                      ->limit(5)
                      ->select();
        $this->assign(array(
            'gonggao'=>$gonggao,
            'help'=>$help
        ));
    }

    protected function page($count, $pageSize='') {
        import('ORG.Util.Page');
        $pageSize = empty($pageSize) ? getSetting('admin_page') : $pageSize;
        $page = new Page($count, $pageSize);
        $page->setConfig('theme', '共 %totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
        $this->limit = $page->firstRow.','.$page->listRows;
        $this->assign('page', $page->show());
    }

    protected function showBanner() {
        $bn = M('Banner');
        $topBanner = $bn->field(array('link', 'name', 'pic'))
                        ->where('is_active AND type=1')
                        ->order('date DESC')
                        ->limit(2)
                        ->select();

        //如果不够2项则用空数组替代
        if(count($topBanner) < 2) array_push($topBanner, array());
        if(empty($topBanner)) {
            $topBanner = array();
            for($i=0;$i<2;$i++) {
                array_push($topBanner, array());
            }
        }

        $rollBanner = $bn->field(array('link', 'name', 'pic'))
                         ->where('is_active AND type=2')
                         ->order('date DESC')
                         ->limit(3)
                         ->select();

        //如果不够3项则用空数组替代
        if(count($rollBanner) < 3) {
            for($i=count($rollBanner);$i<3;$i++) {
                array_push($rollBanner, array());
            }
        }
        if(empty($rollBanner)) {
            $rollBanner = array();
            for($i=0;$i<3;$i++) {
                array_push($rollBanner, array());
            }
        }


        $this->assign(array(
            'topbanner'=>$topBanner,
            'rollbanner'=>$rollBanner
        ));
    }

    // memcached
    protected function useMemcached()
    {
        $memcache = new Memcache();
        $memcache->connect('localhost', 11211);
        return $memcache;
    }

    //显示友情链接
    private function _showFlink() {
        $fl = M('Flink');
        $flinks = $fl->field(array('url', 'name'))
                     ->where('is_active=1')
                     ->order('date DESC')
                     ->select();
        $this->assign('flinks', $flinks);
    }

    private function _showNav() {
        $infoNav = D('InfoNav', 'Admin');
        $this->assign('teninfonav', $infoNav->field(array('id', 'name'))
                                            ->where('is_active=1 AND pid=0')
                                            ->order('sort ASC')
                                            ->limit(6)
                                            ->select());
    }

    //显示网站配置
    private function _showSetting() {
        $sets = getSetting();
        $this->assign('sets', $sets);
    }

    private function _showNow() {
        $now = date('Y年m月d日 H时i分');
        $hour = date('H');
        switch ($hour) {
            case $hour>=6 && $hour<12:
                $this->assign('hello', '早上好');
                break;
            case $hour>=12 && $hour<18:
                $this->assign('hello', '下午好');
                break;
            case $hour>=18 && $hour<24:
                $this->assign('hello', '晚上好');
                break;
            case $hour>=0 && $hour<6:
                $this->assign('hello', '凌晨好');
                break;
        }
        $this->assign('now', $now);
    }







}